//
//  GKDYVideoPortraitView.m
//  GKDYVideo
//
//  Created by QuintGao on 2023/3/17.
//  Copyright © 2023 QuintGao. All rights reserved.
//

#import "GKDYVideoPortraitView.h"
#import "GKDYVideoItemButton.h"
#import "GKDoubleLikeView.h"
#import "UIButton+ImageAlign.h"
#import "NSString+Number.h"

@interface GKDYVideoPortraitView()
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) GKDYVideoItemButton *chapterBtn;
@property (nonatomic, strong) GKDoubleLikeView *doubleLikeView;
@property (nonatomic, strong) NSDate *lastDoubleTapTime;
@end

@implementation GKDYVideoPortraitView

@synthesize player = _player;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    [self addSubview:self.bottomView];
    [self.bottomView addSubview:self.slider];
    [self.bottomView addSubview:self.nameLabel];
    [self.bottomView addSubview:self.chapterBtn];
    [self.bottomView addSubview:self.likeView];
    [self addSubview:self.playBtn];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(300);
    }];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(20);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.slider);
        make.right.equalTo(self).offset(-80);
        make.bottom.equalTo(self.slider.mas_top).offset(-20);
    }];
    [self.chapterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self);
        make.bottom.equalTo(self.slider.mas_top).offset(-10);
        make.width.height.mas_equalTo(60);
    }];
    [self.likeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.chapterBtn);
        make.bottom.equalTo(self.chapterBtn.mas_top).offset(-20);
        make.width.height.mas_equalTo(60);
    }];
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
}

#pragma mark - ZFPlayerMediaControl
- (void)gestureSingleTapped:(ZFPlayerGestureControl *)gestureControl {
    CGFloat diff = [NSDate date].timeIntervalSince1970 - self.lastDoubleTapTime.timeIntervalSince1970;
    if (diff < 0.6) {
        [self handleDoubleTapped:gestureControl.singleTap];
        self.lastDoubleTapTime = [NSDate date];
    }else {
        [self handleSingleTapped];
    }
}

- (void)gestureDoubleTapped:(ZFPlayerGestureControl *)gestureControl {
    [self handleDoubleTapped:gestureControl.doubleTap];
    self.lastDoubleTapTime = [NSDate date];
}

- (void)handleSingleTapped {
    id<ZFPlayerMediaPlayback> manager = self.player.currentPlayerManager;
    if (manager.isPlaying) {
        [manager pause];
        [self.slider showLargeSlider];
        self.playBtn.hidden = NO;
        self.playBtn.transform = CGAffineTransformMakeScale(3, 3);
        [UIView animateWithDuration:0.15 animations:^{
            self.playBtn.alpha = 1;
            self.playBtn.transform = CGAffineTransformIdentity;
        }];
    }else {
        [manager play];
        [self.slider showSmallSlider];
        [UIView animateWithDuration:0.15 animations:^{
            self.playBtn.alpha = 0;
        } completion:^(BOOL finished) {
            self.playBtn.hidden = YES;
        }];
    }
}

- (void)handleDoubleTapped:(UITapGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:gesture.view];
    [self.doubleLikeView createAnimationWithPoint:point view:gesture.view];
    [self.likeView startAnimationWithIsLike:YES];
    if (!self.model.isLike) {
        if ([self.delegate respondsToSelector:@selector(didClickLike:)]) {
            [self.delegate didClickLike:self.model];
        }
    }
    self.model.isLike = YES;
    
}

- (void)setModel:(WSVideo *)model {
    _model = model;
    self.nameLabel.text = WSSF(@"%@", WSEmptyStr(model.title));
    [self.likeView setupLikeCount:[NSString ws10Thousands1DecimalWithValue:model.like_num]];
    [self.likeView setupLikeState:model.isLike];
}

#pragma mark - Action
- (void)chapterDidClick {
    if ([self.delegate respondsToSelector:@selector(didClickChapter:)]) {
        [self.delegate didClickChapter:self.model];
    }
}

- (void)likeDidClick {
    self.model.isLike = !self.model.isLike;
    if ([self.delegate respondsToSelector:@selector(didClickLike:)]) {
        [self.delegate didClickLike:self.model];
    }
}

- (void)sliderDragging:(BOOL)isDragging {
//    self.bottomView.alpha = !isDragging;
}

- (void)willBeginDragging {
    self.bottomView.alpha = 0.4;
}

- (void)didEndDragging {
    self.bottomView.alpha = 1;
}

#pragma mark - Lazy
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
    }
    return _bottomView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont boldSystemFontOfSize:16];
        _nameLabel.textColor = UIColor.font_white_ffffff;
        _nameLabel.userInteractionEnabled = YES;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userDidClick)];
//        [_nameLabel addGestureRecognizer:tap];
    }
    return _nameLabel;
}

- (GKDYVideoSlider *)slider {
    if (!_slider) {
        _slider = [[GKDYVideoSlider alloc] init];
        
        WeakSelf
        _slider.slideBlock = ^(BOOL isDragging) {
            [weakSelf sliderDragging:isDragging];
        };
    }
    return _slider;
}

- (GKDYVideoItemButton *)chapterBtn {
    if (!_chapterBtn) {
        _chapterBtn = [[GKDYVideoItemButton alloc] init];
        [_chapterBtn setImage:[UIImage imageNamed:@"icon_xj"] forState:UIControlStateNormal];
        _chapterBtn.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        [_chapterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_chapterBtn setTitle:APPLocalized(@"選集") forState:(UIControlStateNormal)];
        [_chapterBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleTop) padding:2.5];
        [_chapterBtn addTarget:self action:@selector(chapterDidClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chapterBtn;
}

- (GKLikeView *)likeView {
    if (!_likeView) {
        _likeView = [[GKLikeView alloc] init];
        WeakSelf
        _likeView.likeBlock = ^{
            [weakSelf likeDidClick];
        };
    }
    return _likeView;
}

- (UIButton *)playBtn {
    if (!_playBtn) {
        _playBtn = [[UIButton alloc] init];
        [_playBtn setImage:[UIImage imageNamed:@"icon_play"] forState:UIControlStateNormal];
        _playBtn.userInteractionEnabled = NO;
        _playBtn.hidden = YES;
    }
    return _playBtn;
}

- (GKDoubleLikeView *)doubleLikeView {
    if (!_doubleLikeView) {
        _doubleLikeView = [[GKDoubleLikeView alloc] init];
    }
    return _doubleLikeView;
}

@end
